package com.biz.primus.common.vo;

import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;


/**
 * Created by defei on 5/18/17.
 */
@NoArgsConstructor
@AllArgsConstructor
public class SpringPageVO<T> implements Serializable {

    private static final long serialVersionUID = -6344449330097328246L;

    private List<T> content = Collections.emptyList();

    /**
     * 总条数
     */
    private Long totalElements = 0L;

    /**
     * 每页多少条数据
     */
    private Integer size = 0;

    /**
     * 当前页
     */
    private Integer number = 0;

    /**
     * 总页数
     */
    public Integer getTotalPages() {
        return getSize() == 0 ? 1 : (int) Math.ceil((double) totalElements / (double) getSize());
    }

    /**
     * 是否有上一页
     */
    public Boolean hasPrevious() {
        return number > 0;
    }

    /**
     * 是否有下一页
     */
    public Boolean hasNext() {
        return getNumber() + 1 < getTotalPages();
    }

    /**
     * 当前返回的数据条数
     */
    public Integer getNumberOfElements() {
        return content.size();
    }

    /**
     * {@linkplain SpringPageVO#content}
     */
    public List<T> getContent() {
        return content;
    }

    /**
     * {@linkplain SpringPageVO#content}
     */
    public void setContent(List<T> content) {
        this.content = content;
    }

    /**
     * {@linkplain SpringPageVO#totalElements}
     */
    public Long getTotalElements() {
        return totalElements;
    }

    /**
     * {@linkplain SpringPageVO#totalElements}
     */
    public void setTotalElements(Long totalElements) {
        this.totalElements = totalElements;
    }

    /**
     * {@linkplain SpringPageVO#size}
     */
    public Integer getSize() {
        return size;
    }

    /**
     * {@linkplain SpringPageVO#size}
     */
    public void setSize(Integer size) {
        this.size = size;
    }

    /**
     * {@linkplain SpringPageVO#number}
     */
    public Integer getNumber() {
        return number;
    }

    /**
     * {@linkplain SpringPageVO#number}
     */
    public void setNumber(Integer number) {
        this.number = number;
    }

    public static <T> Builder<T> builder() {
        return new Builder<>();
    }

    public static class Builder<T> {
        SpringPageVO<T> vo = new SpringPageVO<>();

        public Builder<T> content(List<T> content) {
            vo.setContent(content);
            return this;
        }

        public Builder<T> totalElements(Long totalElements) {
            vo.setTotalElements(totalElements);
            return this;
        }

        public Builder<T> size(Integer size) {
            vo.setSize(size);
            return this;
        }

        public Builder<T> number(Integer number) {
            vo.setNumber(number);
            return this;
        }

        public SpringPageVO<T> build() {
            return vo;
        }
    }
}
